package com.usvisatrack.core.factories;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.usvisatrack.core.common.Factory;
import com.usvisatrack.core.dao.model.CheckeeVisa;
import com.usvisatrack.core.dao.model.Visa;
import com.usvisatrack.core.dao.model.VisaClass;
import com.usvisatrack.core.dao.model.VisaStatusLog;

/**
 * Item Data Factory
 * 
 * @author YuCheng Hu
 *
 */
public class VisaFactory extends Factory {
	private static Logger logger = LoggerFactory.getLogger(VisaFactory.class);

	private static final int LISTING_PAGE_SIZE = 20;
	public static final int DEFAULT_AUTH_EXPIRATION = 10;

	/**
	 * Get VISA
	 * 
	 * @param id
	 * @return
	 */
	public static Visa get(Long id) {
		logger.debug("Get Visa OBJ by ID - [{}]", id);
		Factory.beginTransaction();
		Visa item = null;
		try {
			item = Factory.get(Visa.class, id);
			Factory.commitTransaction();
		} catch (Exception ex) {
			item = null;
			logger.error("Get Visa by item ID error", ex);
		} finally {
			Factory.rollbackTransaction();
		}

		return item;
	}

	/**
	 * Get Visa OBJ
	 * 
	 * @param checkkeID
	 * @return
	 */
	public static Visa getVisaFromCheckee(Long checkkeID) {
		logger.debug("Get Visa OBJ by checkkeID - [{}]", checkkeID);
		Visa visa = null;
		CheckeeVisa checkeeVisa = null;

		Factory.beginTransaction();

		try {
			checkeeVisa = Factory.get(CheckeeVisa.class, checkkeID);
			if (checkeeVisa != null) {
				visa = get(checkeeVisa.getVisaID());
			}
			Factory.commitTransaction();
		} catch (Exception ex) {
			logger.error("Get Visa by item ID error", ex);
		} finally {
			Factory.rollbackTransaction();
		}

		return visa;
	}

	/**
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<VisaClass> getAllVisaClass() {
		try {
			Factory.beginTransaction();
			Criteria criteria = Factory.createCriteria(VisaClass.class);
			criteria.setMaxResults(1200);
			return criteria.list();
		} catch (Exception ex) {
			logger.error("search item by item name error", ex);
		} finally {
			Factory.rollbackTransaction();
		}
		return null;
	}
	/**
	 * Save VISA
	 * 
	 * @param visa
	 */
	public static void save(Visa visa) {
		try {
			Factory.beginTransaction();
			Factory.saveOrUpdate(visa);
			Factory.commitTransaction();
		} catch (Exception ex) {
			logger.error("Save Visa OBJ ERROR", ex);
			Factory.rollbackTransaction();
		}
	}

	public static void save(CheckeeVisa checkeeVisa) {
		try {
			Factory.beginTransaction();
			Factory.saveOrUpdate(checkeeVisa);
			Factory.commitTransaction();
		} catch (Exception ex) {
			logger.error("Save CheckeeVisa OBJ ERROR", ex);
			Factory.rollbackTransaction();
		}
	}

	public static void save(VisaStatusLog visaStatusLog) {
		try {
			Factory.beginTransaction();
			Factory.saveOrUpdate(visaStatusLog);
			Factory.commitTransaction();

			updateVisa(visaStatusLog.getVisa());
		} catch (Exception ex) {
			logger.error("Save CheckeeVisa OBJ ERROR", ex);
			Factory.rollbackTransaction();
		}
	}

	private static void updateVisa(Visa visa) {
		try {
			if (visa != null && visa.getId() != null) {
				visa = VisaFactory.get(visa.getId());
			}

			if (visa != null && visa.getVisaStatusLogSet() != null && visa.getVisaStatusLogSet().size() > 0) {
				visa.setVisaStatus(visa.getVisaStatusLogSet().iterator().next().getVisaStatus());
				visa.setModifyDate(new Date());
				VisaFactory.save(visa);
			}

		} catch (Exception ex) {
			logger.error("Save CheckeeVisa OBJ ERROR", ex);
		}
	}

}